package com.freeSoft;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.sql.ResultSetMetaData;
import java.util.Properties;

public class CreateJsp {
	private String tableName;
	private Properties prop;
	private ResultSetMetaData rs;
	private String basePath;
	private String baseJspName;
	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public Properties getProp() {
		return prop;
	}

	public void setProp(Properties prop) {
		this.prop = prop;
	}

	public ResultSetMetaData getRs() {
		return rs;
	}

	public void setRs(ResultSetMetaData rs) {
		this.rs = rs;
	}

	public void createJsp() {
		createAddjsp();
		createEditjsp();
		createListjsp();
	}

	private void createAddjsp() {
		String beanName = tableName.substring(0, 1).toUpperCase()+tableName.substring(1, tableName.length());
		baseJspName = Util.changeName(beanName)+".jsp";
		String outputDir = Util.getOutputDir(prop);
		String projectName = prop.getProperty(Util.PROJECT_NAME);
		basePath = outputDir+ projectName + File.separator+ projectName+ Util.WEB_NAME +File.separator+ 
		"src"+File.separator+"main"+File.separator+"webapp"+File.separator+projectName+File.separator;
		File f= new File(basePath);
		if ( !f.exists())
			f.mkdirs();
		try {
			
			PrintWriter out	   = new PrintWriter(new BufferedWriter( new FileWriter(basePath+"add"+baseJspName)));
			out.println("<%@ page language=\"java\" contentType=\"text/html; charset=utf-8\"    pageEncoding=\"utf-8\"%>");
			out.println("<%@include file=\"header.jsp\"%>");
			out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
			out.println("<html>");
			out.println("<head>");
			out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
			out.println("<title>Add "+beanName +"</title>");
			out.println("</head>");
			out.println("<body>");
			out.println("<form method=\"post\" action=\"add"+Util.changeInitialToUpper(beanName)+"\">");
			for (int i = 1; i <= rs.getColumnCount(); i++) {
				String fieldName = rs.getColumnName(i);
				fieldName = fieldName.substring(0, 1).toUpperCase()	+ fieldName.substring(1, fieldName.length());
				String typeName = rs.getColumnTypeName(i);
				String fieldType = Util.changeJDBCTypeToJavaType(typeName);
				if (fieldType.equals(""))
					fieldType = Util.getDataType(rs.getColumnType(i), rs.getScale(i));
				if ( typeName.toLowerCase().equals("id")){
					out.println(fieldName+":<input type=\"text\"  name=\""+Util.changeInitialToLower(beanName)+"Id\" id=\""+Util.changeInitialToLower(beanName)+"Id\"><br/>");
				}else {
					out.println(fieldName+":<input type=\"text\"  name=\""+fieldName+"\" id=\""+fieldName+"\"><br/>");
				}
				
			}
			out.print("<input type=\"button\"  value=\"Save\"/>&nbsp;");
			out.println("<input type=\"button\"  value=\"reset\"/>");
			out.println("</form>");
			out.println("</body>");
			out.println("</html>");		
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void createEditjsp() {
		String beanName = tableName.substring(0, 1).toUpperCase()+tableName.substring(1, tableName.length());
		baseJspName = Util.changeName(beanName)+".jsp";
		String outputDir = Util.getOutputDir(prop);
		String projectName = prop.getProperty(Util.PROJECT_NAME);
		basePath = outputDir+ projectName + File.separator+ projectName+ Util.WEB_NAME +File.separator+ 
		"src"+File.separator+"main"+File.separator+"webapp"+File.separator+projectName+File.separator;
		File f= new File(basePath);
		if ( !f.exists())
			f.mkdirs();
		try {
			
			PrintWriter out	   = new PrintWriter(new BufferedWriter( new FileWriter(basePath+"edit"+baseJspName)));
			out.println("<%@ page language=\"java\" contentType=\"text/html; charset=utf-8\"    pageEncoding=\"utf-8\"%>");
			out.println("<%@include file=\"header.jsp\"%>");
			out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
			out.println("<html>");
			out.println("<head>");
			out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
			out.println("<title>Edit "+beanName +"</title>");
			out.println("</head>");
			out.println("<body>");
			out.println("<form method=\"post\" action=\"add"+Util.changeInitialToUpper(beanName)+"\">");
			for (int i = 1; i <= rs.getColumnCount(); i++) {
				String fieldName = rs.getColumnName(i);
				fieldName = fieldName.substring(0, 1).toUpperCase()	+ fieldName.substring(1, fieldName.length());
				String typeName = rs.getColumnTypeName(i);
				String fieldType = Util.changeJDBCTypeToJavaType(typeName);
				if (fieldType.equals(""))
					fieldType = Util.getDataType(rs.getColumnType(i), rs.getScale(i));
				if ( typeName.toLowerCase().equals("id")){
					out.println(fieldName+":<input type=\"text\"  name=\""+Util.changeInitialToLower(beanName)+"Id\" id=\""+Util.changeInitialToLower(beanName)+"Id\"><br/>");
				}else {
					out.println(fieldName+":<input type=\"text\"  name=\""+fieldName+"\" id=\""+fieldName+"\"><br/>");
				}				
			}
			out.print("<input type=\"button\"  value=\"Save\"/>&nbsp;");
			out.println("<input type=\"button\"  value=\"reset\"/>");
			out.println("</form>");
			out.println("</body>");
			out.println("</html>");		
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void createListjsp() {

	}
}
